

#import "NSMutableAttributedString+Extend.h"

@implementation NSMutableAttributedString (Extend)


+(NSMutableAttributedString* )setupWithAllString:(NSString*)allString allColor:(UIColor*) allColor allFont:(UIFont*)allFont{
    if (allString.length < 1) {
         NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
        return attributedString;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:allString];
    ///设置总文本字体与颜色
    NSRange originRange = NSMakeRange(0, allString.length);
    [attributedString addAttribute:NSFontAttributeName value:allFont range:originRange];
    [attributedString addAttribute:NSForegroundColorAttributeName value:allColor range:originRange];
    return attributedString;
}


+(NSMutableAttributedString* )setupWithAllString:(NSString*)allString changStringRange:(NSRange)changStringRange allColor:(UIColor*) allColor changeColor:(UIColor*)changeColor allFont:(UIFont*)allFont changeFont:(UIFont*)changFont{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:allString];
    NSRange range1 = NSMakeRange(0, allString.length);
    NSRange range2 = changStringRange;
    
    if (allString.length < (range2.length  + range2.location)) {
        NSLog(@"传入的range 范围错误");
        return nil;
    }
    [attributedString addAttribute:NSFontAttributeName value:allFont range:range1];
    [attributedString addAttribute:NSFontAttributeName value:changFont range:range2];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:allColor
 range:range1];
    [attributedString addAttribute:NSForegroundColorAttributeName value:changeColor
range:range2];
    return attributedString;
}

+(NSMutableAttributedString* )setupWithAllString:(NSString*)allString changeStringRangeArray:(NSMutableArray*)changeStringRangeArray allColor:(UIColor*) allColor changeColor:(UIColor*)changeColor allFont:(UIFont*)allFont changeFont:(UIFont*)changFont{
    
    NSMutableAttributedString *attributedString = [self setupWithAllString:allString allColor:allColor allFont:allFont];
    
    if (changeStringRangeArray.count < 1) {
        return attributedString;
    }
    //判断是否range超长
    for (NSString* changeRangeStr in changeStringRangeArray) {
        NSRange changeRange = NSRangeFromString(changeRangeStr);
        if (allString.length < (changeRange.length  + changeRange.location)) {
            NSLog(@"传入的range 范围错误,大于原string长度");
            return attributedString;
        }
    }
    ///设置需要改变的文本字体与颜色
    for (NSString* changeRangeStr in changeStringRangeArray) {
        NSRange changeRange = NSRangeFromString(changeRangeStr);
        [attributedString addAttribute:NSFontAttributeName value:changFont range:changeRange];
        [attributedString addAttribute:NSForegroundColorAttributeName value:changeColor range:changeRange];
    }
    return attributedString;
}


-(NSMutableAttributedString* )setLineSpacing:(float)lineSpacing nowAttributedString:(NSMutableAttributedString*)nowAttributedString{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing; // 设置行间距
    [nowAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, nowAttributedString.length)];
    return nowAttributedString;
}



@end
